package com.baizhi.springbootshiro.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author Administrator
 */
//@Component
@Slf4j
public class UrlPathMatcherFilter extends PathMatchingFilter {
    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        //基于url权限拦截
        String path = getPathWithinApplication(request);
        log.info("requestPath:{}", path);
        if (!SecurityUtils.getSubject().isAuthenticated()) {
            //未认证
            WebUtils.issueRedirect(request, response, "/login");
            return false;
        }
        if (path.contains("/") || path.contains("/login")) {
            log.info("放行");
            return true;
        }
        //默认拦截所有 下面要进行路径拦截 此处忽略
        return false;

    }
}
